namespace Stee.CAP8.WpfMutipleLanguageLib
{
    using System;
    using WPFLocalizeExtension.Engine;
    using WPFLocalizeExtension.Extensions;
    using System.Globalization;
    public static class LanguageSettings
    {
        private const string USLang = "en-US";
        private const string HKLang = "zh-HK";
        private const string HKLangSuffix = "_HK";
        /// <summary>
        /// 设置初始语言,参数例如"zh-HK"
        /// </summary>
        /// <param name="lang">资源文件名称后缀，例如"zh-HK"</param>
        public static void Initializationlanguage(string lang)
        {
            try
            {
                LocalizeDictionary.Instance.Culture = new CultureInfo(lang);
            }
            catch(NullReferenceException)
            {
                LocalizeDictionary.Instance.Culture = new CultureInfo(USLang);
            }
        }

        /// <summary>
        /// 根据资源名字和key返回当前语言value
        /// </summary>
        /// <param name="key">资源key</param>
        /// <returns></returns>
        public static string GetCurrentValueByKey(string key)
        {
            var localisedValue = LocExtension.GetLocalizedValue<string>(key);
            return localisedValue;
        }

        /// <summary>
        /// 根据key返回指定语言value
        /// </summary>
        /// <param name="lang">指定语言</param>
        /// <param name="key">key</param>
        /// <returns></returns>
        public static string GetSpecifyValueByKey(string lang,string key)
        {
            var localisedValue = string.Empty;
            var locExtension = new LocExtension
            {
                Key = key
            };
            locExtension.ResolveLocalizedValue(out localisedValue, new CultureInfo(lang));
            return localisedValue;
        }

        /// <summary>
        /// 使用model和英文字段返回当前对应语言
        /// </summary>
        /// <param name="model">带有数据的model</param>
        /// <param name="propertyName">英文字段名</param>LocalizeDictionary.Instance.SpecificCulture
        /// <returns></returns>
        public static string GetDataBaseCurrentValue(object model, string propertyName)
        {
            var result = "";
            try
            {
                if (model == null || !model.GetType().IsClass || model.GetType().GetProperty(propertyName) == null)
                {
                    return result;
                }
                var type = model.GetType();
                if (HKLang.Equals(LocalizeDictionary.Instance.Culture.ToString(),StringComparison.Ordinal))
                {
                    result = type.GetProperty(propertyName + HKLangSuffix).GetValue(model) == null ? "" : type.GetProperty(propertyName + HKLangSuffix).GetValue(model).ToString();
                }
                else
                {
                    result = type.GetProperty(propertyName).GetValue(model) == null ? "" : type.GetProperty(propertyName).GetValue(model).ToString();
                }
            }
            catch (NullReferenceException)
            {
                return result;
            }
            return result;
        }
    }
}
